package com.yic.module.mes.service.moroute;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.mes.controller.admin.processrouteinfo.vo.ProcessRouteInfoRespVO;
import com.yic.module.mes.dal.dataobject.itemproduceroute.ItemProduceRouteDO;
import com.yic.module.mes.dal.dataobject.itemproducerouteextend.ItemProduceRouteExtendDO;
import com.yic.module.mes.dal.dataobject.mo.MoDO;
import com.yic.module.mes.service.itemproduceroute.ItemProduceRouteService;
import com.yic.module.mes.service.itemproducerouteextend.ItemProduceRouteExtendService;
import com.yic.module.mes.service.processrouteinfo.ProcessRouteInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.*;
import com.yic.module.mes.controller.admin.moroute.vo.*;
import com.yic.module.mes.dal.dataobject.moroute.MoRouteDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.moroute.MoRouteConvert;
import com.yic.module.mes.dal.mysql.moroute.MoRouteMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 工单工艺路线 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MoRouteServiceImpl implements MoRouteService {

    @Resource
    private MoRouteMapper moRouteMapper;
    @Resource
    private ItemProduceRouteService itemProduceRouteService;
    @Resource
    private ItemProduceRouteExtendService itemProduceRouteExtendService;
    @Resource
    private ProcessRouteInfoService processRouteInfoService;

    @Override
    public Long createMoRoute(MoRouteCreateReqVO createReqVO) {
        // 插入
        MoRouteDO moRoute = MoRouteConvert.INSTANCE.convert(createReqVO);
        moRouteMapper.insert(moRoute);
        // 返回
        return moRoute.getId();
    }

    @Override
    public void batchCreateMoRoute(List<MoDO> mos) {
        //抽取产品工艺路线
        Set<Long> itemProduceRouteIds = CollectionUtils.convertSet(mos, MoDO::getMoRouteId);
        List<ItemProduceRouteDO> itemProduceRouteList = itemProduceRouteService.getItemProduceRouteList(itemProduceRouteIds);
        Map<Long, ItemProduceRouteDO> itemProduceRouteDOMap = CollectionUtils.convertMap(itemProduceRouteList, ItemProduceRouteDO::getId);
        // 抽取产品工艺路线配置
        Set<Long> itemProduceIds = CollectionUtils.convertSet(mos, MoDO::getItemProduceId);
        List<ItemProduceRouteExtendDO> routeExtendS = itemProduceRouteExtendService.getItemProduceRouteExtendListByItemProduceIds(itemProduceIds);
        Map<String, ItemProduceRouteExtendDO> routeExtendMap = CollectionUtils.convertMap(routeExtendS, itemProduceRouteExtendDO -> {
            return itemProduceRouteExtendDO.getItemProduceId() + "-" + itemProduceRouteExtendDO.getRouteId() + "-" + itemProduceRouteExtendDO.getProcessId();
        });
        //抽取工艺路线配置
        Set<Long> routeIds = CollectionUtils.convertSet(itemProduceRouteList, ItemProduceRouteDO::getRouteId);
        List<ProcessRouteInfoRespVO> routeInfos = processRouteInfoService.getProcessRouteInfoListByRouteId(routeIds);
        Map<Long, List<ProcessRouteInfoRespVO>> routeInfoMap = CollectionUtils.convertMultiMap(routeInfos, ProcessRouteInfoRespVO::getRouteId);
        //组装数据
        List<MoRouteDO> moRouteDOS = new ArrayList<>();
        mos.forEach(moDO -> {
            Long routeId = itemProduceRouteDOMap.get(moDO.getMoRouteId()).getRouteId();
            routeInfoMap.get(routeId).forEach(processRouteInfoRespVO -> {
                MoRouteDO moRouteDO = new MoRouteDO(null, moDO.getId(), moDO.getCode(), routeId, processRouteInfoRespVO.getProcessId()
                        , processRouteInfoRespVO.getNextProcessId(), processRouteInfoRespVO.getSubsequence()
                        , processRouteInfoRespVO.getLink(), processRouteInfoRespVO.getMainProcess(), BigDecimal.ZERO, null);
                ItemProduceRouteExtendDO extendDO = routeExtendMap.get(moDO.getItemProduceId() + "-" + routeId + "-" + processRouteInfoRespVO.getProcessId());
                if (ObjectUtil.isNotEmpty(extendDO)) {
                    moRouteDO.setWorkHours(extendDO.getWorkHours());
                    moRouteDO.setProcessFile(extendDO.getProcessFile());
                }
                moRouteDOS.add(moRouteDO);
            });
        });
        moRouteMapper.insertBatch(moRouteDOS);
    }

    @Override
    public void updateMoRoute(MoRouteUpdateReqVO updateReqVO) {
        // 校验存在
        validateMoRouteExists(updateReqVO.getId());
        // 更新
        MoRouteDO updateObj = MoRouteConvert.INSTANCE.convert(updateReqVO);
        moRouteMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoRoute(Long id) {
        // 校验存在
        validateMoRouteExists(id);
        // 删除
        moRouteMapper.deleteById(id);
    }

    @Override
    public void deleteMoRouteByMoId(Long id) {
        moRouteMapper.deleteByMoId(id);
    }

    private void validateMoRouteExists(Long id) {
        if (moRouteMapper.selectById(id) == null) {
            throw exception(MO_ROUTE_NOT_EXISTS);
        }
    }

    @Override
    public MoRouteDO getMoRoute(Long id) {
        return moRouteMapper.selectById(id);
    }

    @Override
    public List<MoRouteDO> getMoRouteList(Collection<Long> ids) {
        return moRouteMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MoRouteDO> getMoRoutePage(MoRoutePageReqVO pageReqVO) {
        return moRouteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MoRouteDO> getMoRouteList(MoRouteExportReqVO exportReqVO) {
        return moRouteMapper.selectList(exportReqVO);
    }

    @Override
    public List<MoRouteDO> getMoRouteByMoId(Long id) {
        return moRouteMapper.selectListByMoId(id);
    }

    @Override
    public boolean checkUseProcess(Long processId) {
        MoRouteDO moRouteDO = moRouteMapper.selectOneByProcessId(processId);
        return ObjectUtil.isNotEmpty(moRouteDO);
    }

}
